package com.neuedu.helloworld.ticket;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.neuedu.helloworld.R;
import com.neuedu.helloworld.order.OrderDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Ticket2Activity extends AppCompatActivity {


    ListView lvSeatItems;
    Button btnToQrCode;
    List data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket2);

        setTitle("车票预订2/5");

        lvSeatItems = findViewById(R.id.lvSeatItems);
        btnToQrCode = findViewById(R.id.btnToQrCode);


        Map seat1 = new HashMap();
        seat1.put("name","硬座");
        seat1.put("num","145");
        seat1.put("price",55.05);

        data.add(seat1);

        Map seat2 = new HashMap();
        seat2.put("name","软座");
        seat2.put("num","145");
        seat2.put("price",55.05);

        data.add(seat2);



        Map seat3 = new HashMap();
        seat3.put("name","二等座");
        seat3.put("num","145");
        seat3.put("price",new Double(55.05)); //

        data.add(seat3);


        /**
         * Context context, List<? extends Map<String, ?>> data,
         *             @LayoutRes int resource, String[] from, @IdRes int[] to
         */
        lvSeatItems.setAdapter(new SeatListAdapter( data));


        btnToQrCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Ticket2Activity.this, OrderDetailActivity.class);
                startActivity(intent);
            }
        });
    }


    class SeatListAdapter extends BaseAdapter {

        private List<Map> seatData;

        public SeatListAdapter(List data) {
            this.seatData = data;
        }

        @Override
        public int getCount() {
            return seatData.size();
        }

        @Override
        public Object getItem(int position) {
            return seatData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        // position  0
        //渲染单行
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View view = null;

            ViewHolder holder = null;
            if (convertView == null) {  //第一次 执行findViewByID
                view = inflater.inflate(R.layout.activity_ticket2_list_item, parent, false);

                holder = new ViewHolder();
                holder.tvSeatName = view.findViewById(R.id.tvSeatName);
                holder.tvSeatNum = view.findViewById(R.id.tvSeatNum);
                holder.tvSeatPrice = view.findViewById(R.id.tvSeatPrice);

                holder.btnBootTicket = view.findViewById(R.id.btnBootTicket);

                //设置单行文本的，  按钮事件

                 view.setTag(holder);



            } else { //二次渲染 ++
                view = convertView;
                holder = (ViewHolder) view.getTag();

            }

            Map<String ,Object> seat = seatData.get(position);
            String name = (String) seat.get("name");
            String num = (String) seat.get("num");
            double  price  = (double) seat.get("price");



            //重新设置 文字、按钮的事件
            holder.tvSeatName.setText(name);
            holder.tvSeatNum.setText(num+"张");
            holder.tvSeatPrice.setText(String.valueOf(price)+"￥");


            holder.btnBootTicket.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(Ticket2Activity.this, "name : "+name, Toast.LENGTH_SHORT).show();
                }
            });


            return view;
        }


        class ViewHolder {
            TextView tvSeatName;
            TextView tvSeatNum;
            TextView tvSeatPrice;
            Button btnBootTicket;
        }


    }


}